$(document).ready(function(){
$(function() {
	validateKickout();
    validateRule();
	$('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
});

$.validator.setDefaults({
    submitHandler: function() {
    	validate('#username', '#signupForm');
    	validate('#password', '#signupForm');
    	validate('#validateCode', '#signupForm');
		login();
    }
});

function login() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "rememberMe": rememberMe
        },
        success: function(r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    $('#signupForm').validate({
        errorPlacement: function errorPlacement(error, element) {
          error.addClass("has-error has-error-msg");
          error.appendTo(element.parent());
        },
        onkeyup: false,
        onclick: false,
        onfocusout: false,
        rules: {
          username: "required",
          password: "required",
          validateCode: "required"
        },
        messages: {
          username: "请输入您的用户名",
          password: "请输入您的密码",
          validateCode: "请输入验证码"
        }
      });
}

function validate(selector, form) {
    var flag = $(form).validate().element($(selector));

    if (flag) {
      $(selector).parents(".login-input-content").removeClass("has-error");
    } else {
      $(selector).parents(".login-input-content").addClass("has-error");
    }

    return flag;
  }
 $('#username').keyup(function () {
    validate('#username', '#signupForm');
  });
  $('#password').keyup(function () {
    var flag = validate('#password', '#signupForm');
    if (flag) {
      $(".look").css('display', 'block');
    } else {
      $(".look").css('display', 'none');
    }
  });
  $('#validateCode').keyup(function () {
    validate('#validateCode', '#signupForm');
  });
  
  function toggleLook() {
	    $(".look").toggleClass("glyphicon-eye-open");
	    $(".look").toggleClass("glyphicon-eye-close");
	  }

  $(".look").mousedown(function () {
    $('#password').attr("type", "text");
    toggleLook();
  });
  $(".look").mouseup(function () {
    $('#password').attr("type", "password");
    toggleLook();
  });
  $(".look").mouseout(function () {
    if ($(".look").hasClass("glyphicon-eye-open")) {
      $('#password').attr("type", "password");
      toggleLook();
    }
  });
function validateKickout() {
	if (getParam("kickout") == 1) {
	    layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
	        icon: 0,
	        title: "系统提示"
	    },
	    function(index) {
	        //关闭弹窗
	        layer.close(index);
	        if (top != self) {
	            top.location = self.location;
	        } else {
	            var url  =  location.search;
	            if (url) {
	                var oldUrl  = window.location.href;
	                var newUrl  = oldUrl.substring(0,  oldUrl.indexOf('?'));
	                self.location  = newUrl;
	            }
	        }
	    });
	}
}

function getParam(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}
})